
public class e2board {

    int colnb;
    int rownb;
    e2piece[][] piecetab;

    public e2board(int in_colnb, int in_rownb) {
        colnb = in_colnb;
        rownb = in_rownb;
        int i, j;
        piecetab = new e2piece[colnb][rownb];
        for (i = 0; i < colnb; i++) {
            for (j = 0; j < rownb; j++) {
                piecetab[i][j] = null;
            }
        }
    }

    public boolean place_piece_at(e2piece in_piece, int in_col, int in_row, int rotation) {
        boolean result;
        result = true;
        if (piecetab[in_col][in_row] == null) {
            piecetab[in_col][in_row] = in_piece;
            in_piece.set_position(in_row,in_col);
            in_piece.rotate(rotation);
        } else {
            result = false;
        }
        return result;
    }

    public boolean unplace_piece_at(int in_col, int in_row) {
        boolean result;
        result = true;
        if (piecetab[in_col][in_row] != null) {
            piecetab[in_col][in_row] = null;
        } else {
            result = false;
        }
        return result;
    }

    public boolean unplace_piece(int in_id) {
        int i, j;
        boolean result;
        result = false;
        for (i = 0; i < colnb; i++) {
            for (j = 0; j < rownb; j++) {
                if (piecetab[i][j] != null) {
                    if (piecetab[i][j].id == in_id) {
                        piecetab[i][j] = null;
                        result = true;
                    }
                }
            }
        }

        return result;
    }

    public e2piece get_piece_at(int in_col, int in_row) {
        return piecetab[in_col][in_row];

    }

    public e2colorpattern get_color_pattern_at(int in_col, int in_row) {
        //return color pattern at given coordinates. 
        //Be careful that the row nb is growing when going down
        e2colorpattern returned_pattern;
        int color_top, color_bottom, color_left, color_right;
        if (in_row == 0) {
            color_top = 1;
        } else if (piecetab[in_col][in_row - 1] != null) {
            color_top = piecetab[in_col][in_row - 1].bottom;
        } else {
            color_top = 0;
        }

        if (in_col == colnb - 1) {
            color_right = 1;
        } else if (piecetab[in_col + 1][in_row] != null) {
            color_right = piecetab[in_col + 1][in_row].left;
        } else {
            color_right = 0;
        }

        if (in_row == rownb - 1) {
            color_bottom = 1;
        } else if (piecetab[in_col][in_row + 1] != null) {
            color_bottom = piecetab[in_col][in_row + 1].top;
        } else {
            color_bottom = 0;
        }

        if (in_col == 0) {
            color_left = 1;
        } else if (piecetab[in_col - 1][in_row] != null) {
            color_left = piecetab[in_col - 1][in_row].right;
        } else {
            color_left = 0;
        }
        returned_pattern = new e2colorpattern(color_top, color_right, color_bottom, color_left);
        return returned_pattern;

    }
}
